package projectmanagement.console;

import projectmanagement.models.Project;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Dmitry Toryanik
 *         <p/>
 *         Projects handling class handles and renders
 *         menu options which relates to
 *         project's state. Has a custom constructor which
 *         takes the console manager class to traversing
 *         entire application menu.
 */
public class ProjectsHandling {
    private Scanner scanner = new Scanner(System.in);
    private ConsoleManager consoleManager;

    ProjectsHandling(ConsoleManager consoleManager) {
        this.consoleManager = consoleManager;
    }

    void createProjectFromScratch() {
        System.out.println("Creating project from scratch...\n");

        Calendar start = Calendar.getInstance();
        Calendar finish = Calendar.getInstance();

        System.out.print("Project name: ");
        String projectName = scanner.nextLine().trim();

        if (!consoleManager.getManagementTool().isProjectNameValid(projectName)) {
            ConsoleManager.goBackHandler("We already have project by this name!", scanner);
            consoleManager.projectsMenuHandler(null);
        }

        System.out.print("Project start date (MM/dd/yyyy): ");
        String startDate = scanner.nextLine();
        System.out.print("Project finish date (MM/dd/yyyy): ");
        String finishDate = scanner.nextLine();

        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        try {
            start.setTime(formatter.parse(startDate));
            finish.setTime(formatter.parse(finishDate));
        } catch (ParseException e) {
            ConsoleManager.goBackHandler("Date format exception!", scanner);
            createProjectFromScratch();
        }

        System.out.print("Project's description: ");
        String description = scanner.nextLine();

        if (consoleManager.getManagementTool().createProject(projectName, start, finish, description) != null) {
            ConsoleManager.goBackHandler("\nProject was created successfully!", scanner);
            consoleManager.projectsMenuHandler(null);
        } else {
            ConsoleManager.goBackHandler("Project creation error!", scanner);
            consoleManager.projectsMenuHandler(null);
        }
    }

    void showProjectsList() {
        System.out.println("Entire list of projects...\n");

        Collection<Project> projectList = consoleManager.getManagementTool().getProjectsList();
        if (projectList.size() != 0) {
            for (Project p : projectList)
                System.out.println(p + "\n");

            ConsoleManager.goBackHandler("", scanner);
            consoleManager.projectsMenuHandler(null);
        } else {
            ConsoleManager.goBackHandler("We have no projects yet!", scanner);
            consoleManager.projectsMenuHandler(null);
        }
    }

    void goToProject() {
        System.out.println("Existing project searching...\n");

        System.out.print("Project name: ");
        String name = scanner.nextLine();

        Project p = consoleManager.getManagementTool().getProjectByName(name);
        if (p != null) {
            existProjectMenu(p);
        } else {
            ConsoleManager.goBackHandler("We have no project by this name!", scanner);
            consoleManager.projectsMenuHandler(null);
        }
    }

    void existProjectMenu(final Project p) {
        final ScrumHandling scrumHandling = new ScrumHandling(consoleManager, this);

        ConsoleMenu consoleMenu = new ConsoleMenu();
        consoleMenu.add("Create iteration", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.createIteration(p);
            }
        });
        consoleMenu.add("Create task", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.createTask(p);
            }
        });
        consoleMenu.add("Assign task to iteration", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.assignTaskToIteration(p);
            }
        });
        consoleMenu.add("Assign user to task", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.assignUserToTask(p);
            }
        });
        consoleMenu.add("Show iteration's tasks", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.showIterationsTasks(p);
            }
        });
        consoleMenu.add("Call to meeting", new ConsoleMenuCallback() {
            public void invoke() {
                callToMeeting(p);
            }
        });
        consoleMenu.add("Remove current project", new ConsoleMenuCallback() {
            public void invoke() {
                removeCurrentProject(p);
            }
        });
        consoleMenu.add("Go back", new ConsoleMenuCallback() {
            public void invoke() {
                consoleManager.projectsMenuHandler(null);
            }
        });
        consoleMenu.show(p.getProjectName());
    }

    void callToMeeting(Project p) {
        System.out.println("Call to meeting");
        System.out.println("---------------");
        System.out.println("[UNDER CONSTRUCTION]\n");

        ConsoleManager.goBackHandler("", scanner);
        existProjectMenu(p);
    }

    void removeCurrentProject(Project p) {
        System.out.printf("Do you really want to remove project \"%s\"? (Y/N)\n", p.getProjectName());

        String answer = scanner.nextLine();
        if (answer.equalsIgnoreCase("y")) {
            consoleManager.getManagementTool().deleteProject(p);

            ConsoleManager.goBackHandler("Project was successfully removed", scanner);
            consoleManager.projectsMenuHandler(null);
        } else {
            existProjectMenu(p);
        }
    }

}
